      SUBROUTINE BUILD ( STR, TOP, NTOP, BOTTOM, NBOT )
C*
C*                  *******************************
C*                  *******************************
C*                  **                           **
C*                  **          BUILD            **
C*                  **                           **
C*                  *******************************
C*                  *******************************
C*
C*     SUBPROGRAM :
C*          BUILD OUTPUT LINE
C*
C*     AUTHOR :
C*          ART RAGOSTA
C*          MS 207-5
C*          AMES RESEARCH CENTER
C*          MOFFETT FIELD, CA   94035
C*          (415) 694-5578
C*
C*     PURPOSE :
C*          BUILD THE STRING OF OUTPUT UNITS, CANCELLING UNITS ON TOP
C*          AND BOTTOM.
C*
C*     INPUT ARGUMENTS :
C*          TOP    - UNITS WHICH ARE IN NUMERATOR
C*          NTOP   - NUMBER IN TOP
C*          BOTTOM - UNITS IN DENOMINATOR
C*          NBOT   - NUMBER IN BOTTOM
C*
C*     OUTPUT ARGUMENTS :
C*          STR  - THE TOTAL STRING OF OUTPUT UNITS
C*
C*     INTERNAL WORK AREAS :
C*          TSTR - USED TO SIMPLIFY '**N' CALCULATIONS
C*
C*     COMMON BLOCKS :
C*          NONE
C*
C*     FILE REFERENCES :
C*          NONE
C*
C*     SUBPROGRAM REFERENCES :
C*          LEFT, LENGTH
C*
C*     ERROR PROCESSING :
C*          NONE
C*
C*     TRANSPORTABILITY LIMITATIONS :
C*          NONE
C*
C*     ASSUMPTIONS AND RESTRICTIONS :
C*          NONE
C*
C*     LANGUAGE AND COMPILER :
C*          ANSI FORTRAN 77
C*
C*     VERSION AND DATE :
C*          VERSION I.0     13-SEP-85
C*
C*     CHANGE HISTORY :
C*          13-SEP-85    INITIAL VERSION
C*
C***********************************************************************
C*
      CHARACTER *500 TSTR
      CHARACTER *(*) STR
      CHARACTER *6 TOP(40), BOTTOM(40), WORK
C
      STR = ' '
      IS = 1
C
C --- DELETE DUPLICATE ENTRIES ON TOP AND BOTTOM
C
      I = 1
10    IF (NTOP .GT. 0) THEN
         DO 20 J = 1, NBOT
            IF (TOP(I) .EQ. BOTTOM(J)) THEN
               BOTTOM(J) = BOTTOM(NBOT)
               TOP(I) = TOP(NTOP)
               NTOP = NTOP - 1
               NBOT = NBOT - 1
               IF (I .LE. NTOP) THEN
                  GO TO 10
               ELSE
                  GO TO 30
               ENDIF
            ENDIF
20          CONTINUE
         I = I + 1
         IF (I .LE. NTOP) GO TO 10
      ENDIF
C
C --- REPLACE MULTIPLE ENTRIES WITH '**'N, ADD TOP UNITS TO STRING
C
30    I = 1
35    IF (I .LE. NTOP) THEN
         STR(IS:) = TOP(I)
         IS = IS + LENGTH(TOP(I))
         STR(IS:IS) = '*'
         IS = IS + 1
         IC = 1
         J = I + 1
40       IF (J .LE. NTOP) THEN
            IF (TOP(I) .EQ. TOP(J)) THEN
               IC = IC + 1
               TOP(J) = TOP(NTOP)
               NTOP = NTOP - 1
               GO TO 40
            ENDIF
            J = J + 1
            GO TO 40
         ENDIF
C
C ----- IF THERE WERE MORE THAN ONE, REPLACE FIRST WITH **N
C
         IF (IC .GT. 1) THEN
            WRITE(WORK,900) IC
            CALL LEFT ( WORK )
            TSTR = '*' // WORK(1:LENGTH(WORK)) // '*'
            STR(IS:) = TSTR
            IS = IS + LENGTH(TSTR)
         ENDIF
         I = I + 1
         GO TO 35
      ENDIF
      IF ( NTOP .EQ. 0 ) THEN
         STR = '1*'
         IS = 3
      ENDIF
C
C --- REPLACE LAST '*' WITH '/' UNLESS THERE IS NO DENOMINATOR
C
      IF (NBOT .LE. 0) THEN
         IF (NTOP .EQ. 0) THEN
            STR = 'N.D.'
            RETURN
         ENDIF
         STR(IS-1:IS-1) = ' '
      ELSE
         STR(IS-1:IS-1) = '/'
C
C --- REPLACE MULTIPLE ENTRIES WITH '**'N, ADD BOTTOM UNITS TO STRING
C
         I = 1
45       IF (I .LE. NBOT) THEN
            STR(IS:) = BOTTOM(I)
            IS = IS + LENGTH(BOTTOM(I))
            STR(IS:IS) = '*'
            IS = IS + 1
            IC = 1
            J = I + 1
50          IF (J .LE. NBOT) THEN
               IF (BOTTOM(I) .EQ. BOTTOM(J)) THEN
                  IC = IC + 1
                  BOTTOM(J) = BOTTOM(NBOT)
                  NBOT = NBOT - 1
                  GO TO 50
               ENDIF
               J = J + 1
               GO TO 50
            ENDIF
C
C ----- IF THERE WERE MORE THAN ONE, REPLACE FIRST WITH **N
C
            IF (IC .GT. 1) THEN
               WRITE(WORK,900) IC
               CALL LEFT ( WORK )
               TSTR = '*' // WORK(1:LENGTH(WORK)) // '*'
               STR(IS:) = TSTR
               IS = IS + LENGTH(TSTR)
            ENDIF
            I = I + 1
            GO TO 45
         ENDIF
      STR(IS-1:IS-1) = ' '
      ENDIF
      RETURN
900   FORMAT(I6)
      END
C
C---END BUILD
C
